/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.library.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.library.entity.Library;
import com.thinkgem.jeesite.modules.library.service.LibraryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 图书馆信息Controller
 * @author zhangc
 * @version 2018-01-07
 */
@Controller
@RequestMapping(value = "${adminPath}/library/library")
public class LibraryController extends BaseController{

	@Autowired
	private LibraryService libraryService;
	
	@ModelAttribute
	public Library get(@RequestParam(required=false) String id) {
		Library entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = libraryService.get(id);
		}
		if (entity == null){
			entity = new Library();
		}
		return entity;
	}
	
	@RequiresPermissions("library:library:view")
	@RequestMapping(value = {"list", ""})
	public String list(Library library, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Library> page = libraryService.findPage(new Page<Library>(request, response), library);
		model.addAttribute("page", page);
		return "modules/library/libraryList";
	}

	@RequiresPermissions("library:library:view")
	@RequestMapping(value = "form")
	public String form(Library library, Model model) {
		model.addAttribute("library", library);
		return "modules/library/libraryForm";
	}

	@RequiresPermissions("library:library:edit")
	@RequestMapping(value = "save")
	public String save(Library library, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, library)){
			return form(library, model);
		}
		libraryService.save(library);
		addMessage(redirectAttributes, "保存图书馆信息成功");
		return "redirect:"+ Global.getAdminPath()+"/library/library/?repage";
	}
	
	@RequiresPermissions("library:library:edit")
	@RequestMapping(value = "delete")
	public String delete(Library library, RedirectAttributes redirectAttributes) {
		libraryService.delete(library);
		addMessage(redirectAttributes, "删除图书馆信息成功");
		return "redirect:"+ Global.getAdminPath()+"/library/library/?repage";
	}

}